import SparkMD5 from "spark-md5";

export const getImgUrl=(path)=>{
    return import.meta.env.VITE_PIC_BASE_URL+path
}

export const onUpload = async (File) => {
    const reader = new FileReader();
    // 读取文件内容，结果用data:url的字符串形式表示
    reader.readAsArrayBuffer(File.raw);
    reader.onload = function (e) {
        //这个是获取到byte数组
        console.log(e.target.result);
        //basedata是之前定义的变量
        const basedata = e.target.result;
        const binaryData = new Uint8Array(basedata);
        console.log(binaryData);
        console.log("二进制转化完成")
    }
}

export function fileParse(file, type) {
    const caseType = {
        'base64': 'readAsDataURL',
        'buffer': 'readAsArrayBuffer'
    }
    const fileRead = new FileReader()
    return new Promise(resolve => {
        fileRead[caseType[type]](file)
        fileRead.onload = (res) => {
            resolve(res.target.result)
        }
    })
}

export async function  getFileMD5(file){
    let buffer = await fileParse(file, "buffer")
    const spark = new SparkMD5.ArrayBuffer()
    spark.append(buffer);
    let md5 = spark.end();
    return md5
}
//使用方法

// fileParse(File.raw, 'buffer')